package com.wise.wing.activity.login;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import cn.jpush.android.api.JPushInterface;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.bm.corelibs.logic.BaseLogic.NListener;
import com.bm.corelibs.utils.ToastMgr;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.wise.wing.R;
import com.wise.wing.activity.logic.LoginManager;
import com.wise.wing.activity.main.BaseActivity;
import com.wise.wing.activity.main.MainActivity;
import com.wise.wing.activity.tools.Tools;
import com.wise.wing.bean.BaseData;
import com.wise.wing.bean.MapData;
import com.wise.wing.constant.Constant;
import com.wise.wing.constant.Urls;
import com.wise.wing.views.CommonNavigationBar;

public class LoginActivity extends BaseActivity implements OnClickListener {
	protected static final String TAG = "LoginActivity";
	CommonNavigationBar bar;
	EditText login_tel;
	EditText login_psw;
	Button login_sure;
	Button login_new;
	TextView forget_textview;
	LoginManager manager = new LoginManager();
	boolean fromCreateOrder;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		findViews();
		init();
		addListeners();
	}

	@Override
	public void findViews() {
		bar = (CommonNavigationBar) findViewById(R.id.navi);
		login_tel = (EditText) findViewById(R.id.login_tel);
		login_psw = (EditText) findViewById(R.id.login_psw);
		login_sure = (Button) findViewById(R.id.login_sure);
		login_new = (Button) findViewById(R.id.login_new);
		forget_textview = (TextView) findViewById(R.id.forget_textview);
	}

	@Override
	public void init() {
		bar.setTitle("站点登录");
		fromCreateOrder = getIntent().getBooleanExtra("fromCreateOrder", false);
		bar.setListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(mActivity, MainActivity.class));
				finish();
			}
		});
	}

	@Override
	public void addListeners() {
		login_sure.setOnClickListener(this);
		login_new.setOnClickListener(this);
		forget_textview.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.login_sure:
			if (Tools.validatePhone(Tools.getTextViewText(login_tel))) {
				if (Tools.getTextViewText(login_psw).length() >= 6) {
					// loadingDialog.show();
					/*
					 * manager.login(Tools.getTextViewText(login_tel),
					 * Tools.getTextViewText(login_psw), loginListener());
					 */
					HttpUtils http = new HttpUtils();
					RequestParams params = new RequestParams();
					params.addBodyParameter("username",
							Tools.getTextViewText(login_tel));
					params.addBodyParameter("password",
							Tools.getTextViewText(login_psw));
					params.addBodyParameter("role", Constant.ROLE);
					http.send(HttpMethod.POST, Urls.LOGIN, params,
							new RequestCallBack<String>() {

								@Override
								public void onFailure(HttpException arg0,
										String arg1) {
									// TODO Auto-generated method stub

								}

								@Override
								public void onSuccess(
										ResponseInfo<String> responseInfo) {
									// TODO Auto-generated method stub
									// String result=responseInfo.result;
									String result = responseInfo.result;
									Log.i(TAG, result);

									try {
										JSONObject json = new JSONObject(result);
										int statues = json.getInt("status");
										String data = json.getString("data");
										MapData parseObject = JSON.parseObject(data,MapData.class);
									
										
										if (1 == statues) {
											Toast.makeText(LoginActivity.this,
													"登陆成功", Toast.LENGTH_LONG)
													.show();
											LoginManager.getInstance(mActivity).saveUserInfo(parseObject.dispatcher);
											Intent intent = new Intent(
													LoginActivity.this,
													MainActivity.class);
											startActivity(intent);
											finish();
										
											

										}
									} catch (JSONException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();

									}

								}
							});

				} else {
					Toast.makeText(this, "请输入6-16位密码", Toast.LENGTH_SHORT)
							.show();
				}

			} else {
				Toast.makeText(this, "请输入规范的手机号码 ", Toast.LENGTH_SHORT).show();
			}

			break;
		case R.id.login_new:
			startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
			break;
		case R.id.forget_textview:
			startActivity(new Intent(LoginActivity.this,
					ForgetPasswordActivity.class));
			break;

		default:
			break;
		}

	}

	private NListener<BaseData> loginListener() {
		return new NListener<BaseData>() {
			@Override
			public void onResponse(BaseData data) {
				loadingDialog.dismiss();
				if (data.status == Constant.STATUS_OK) {
					if (data.data.dispatcher != null)
						LoginManager.getInstance(mActivity).saveUserInfo(
								data.data.dispatcher);
					JPushInterface.setAlias(mActivity, data.data.dispatcher.id
							+ "", null);
					if (fromCreateOrder) {
						System.out.println("fromCreateOrder" + fromCreateOrder);
						setResult(RESULT_OK);
						finish();
					} else {
						startActivity(new Intent(LoginActivity.this,
								MainActivity.class));
						finish();
					}
				} else {
					ToastMgr.show(data.msg);
				}
			}

			@Override
			public void onErrResponse(VolleyError error) {
				loadingDialog.dismiss();
			}

			@Override
			public void onAllPageLoaded(int nowPage, int totalPage) {
			}
		};
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			startActivity(new Intent(mActivity, MainActivity.class));
			finish();
		}

		return super.onKeyDown(keyCode, event);
	}
}
